package com.example.blog.modules.article.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.article.dto.TagDTO;
import com.example.blog.modules.article.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Tag(name = "标签管理", description = "标签管理相关接口")
@RestController
@RequestMapping("/api/tags")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    @Operation(summary = "创建标签")
    @PostMapping
    @HasPermission("tag:create")
    public ApiResponse<TagDTO> createTag(@Valid @RequestBody TagDTO tagDTO) {
        return ApiResponse.success(tagService.createTag(tagDTO));
    }

    @Operation(summary = "更新标签")
    @PutMapping("/{id}")
    @HasPermission("tag:update")
    public ApiResponse<TagDTO> updateTag(@PathVariable Long id, @Valid @RequestBody TagDTO tagDTO) {
        return ApiResponse.success(tagService.updateTag(id, tagDTO));
    }

    @Operation(summary = "删除标签")
    @DeleteMapping("/{id}")
    @HasPermission("tag:delete")
    public ApiResponse<Void> deleteTag(@PathVariable Long id) {
        tagService.deleteTag(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取标签详情")
    @GetMapping("/{id}")
    @HasPermission("tag:read")
    public ApiResponse<TagDTO> getTag(@PathVariable Long id) {
        return ApiResponse.success(tagService.getTag(id));
    }

    @Operation(summary = "获取所有标签")
    @GetMapping("/all")
    @HasPermission("tag:read")
    public ApiResponse<List<TagDTO>> getAllTags() {
        return ApiResponse.success(tagService.getAllTags());
    }

    @Operation(summary = "根据ID列表获取标签")    
    @GetMapping("/by-ids")
    @HasPermission("tag:read")
    public ApiResponse<List<TagDTO>> getTagsByIds(@RequestParam Set<Long> ids) {
        return ApiResponse.success(tagService.getTagsByIds(ids));
    }

    @Operation(summary = "获取标签列表")
    @GetMapping
    @HasPermission("tag:read")
    public ApiResponse<Page<TagDTO>> getTags(
            @RequestParam(required = false) String keyword,
            Pageable pageable) {
        return ApiResponse.success(tagService.getTags(keyword, pageable));
    }
} 